/**
 * Created by ybjaa on 2017/4/27.
 */

import {Directive, ElementRef, HostListener, Input} from "@angular/core";
@Directive({
    selector:"[validClass]"
})
export class ClassChangeDirective{

    @Input("validClass") val:boolean;


    constructor(private element:ElementRef){}



    /*@HostListener("input") onInput(){
        this.handle();
    }

    @HostListener("keyup") onKeyUp(){
        this.handle();
    }*/

    @HostListener("invalid") onInvalid()
    {
        this.handle();
    }


    handle(){
        let classList=this.element.nativeElement.parentElement.classList;

        if(this.val)
        {
            classList.remove("has-error");
            //classList.remove("ng-invalid");
            //this.element.nativeElement.classList.add("has-success");
        }
        else {
            //this.element.nativeElement.classList.remove("has-success");
            classList.add("has-error");
        }
    }

}